// 公共接口
import {
  request,
  getBaseUrl,
} from '../request';

// 上传接口
export const FileUploadPublic = (params) => {
  let baseUrl = getBaseUrl() || '';
  if (!baseUrl.endsWith('/')) {
    baseUrl = baseUrl + '/'
  }
  // 真机
  // file:///storage/emulated/0/Android/data/io.dcloud.HBuilder/apps/HBuilder/doc/uniapp_temp/compressed/1737100393198_1736478173163.jpg 从相册选择
  // _doc/uniapp_temp_1737100427771/camera/1737100440892.jpg 拍照
  // _doc/uniapp_temp_1737100286263/recorder/1737100338175.mp3  录音
  // _doc/uniapp_temp_1737100475151/camera/1737100477512.mp4 视频

  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: baseUrl + 'eis.gridfileload?service=appuService&method=upload&loadflag=upload',
      filePath: params.filePath,
      // ...fileParam,
      name: 'file', // 服务器端接收文件的字段名
      header: {
        'Referer': baseUrl + '/eis.form?service=archiveQueryService&method=page',
        'X-Requested-With': 'XMLHttpRequest'
      },
      success: (res) => {
        console.log('上传成功1', res);
        resolve(res)
      },
      fail: (err) => {
        console.error('上传失败1', err);
        reject(err);
      }
    });
  })
}

// 选择上传图片
export const FileUploadImage = () => {
  return new Promise((resolve, reject) => {
    uni.chooseImage({
      count: 3,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['camera', 'album'], // 'album':相册
      success: (res) => {
        console.log('选择图片', res);
        resolve(res.tempFiles || []);
      }
    })
  })
}

// 选择上传视频
export const FileUploadVideo = () => {
  return new Promise((resolve, reject) => {
    uni.chooseVideo({
      sourceType: ['camera'],
      maxDuration: 30,
      compressed: true,
      quality: 'medium', // 压缩质量:中
      success: (res) => {
        uni.showLoading({
          title: '上传中...',
          mask: true,
        });

        FileUploadPublic({
            filePath: res.tempFilePath
          })
          .then((res) => {
            if (res.statusCode === 200) {
              const backData = JSON.parse(res.data);
              if (backData.code === '0') {
                resolve(backData.data);
              } else {
                reject({
                  msg: 'video backData err',
                  res: res
                });
              }
            } else {
              reject({
                msg: 'video res err',
                res: res
              });
            }
          })
          .catch((err) => {
            reject(err);
          })
          .finally(() => {
            uni.hideLoading();
          });
      },
    });
  });
};

// 数据字典
export const getDict = (params, otherObj) => {
  const data = Object.assign({}, {
    spell: '',
    categoryType: '',
    number: 1000,
  }, params);
  let url = 'eis.simple?service=eamPageParamQueryService&method=getEamXmSelect';
  if (otherObj) {
    url = otherObj.url || url;
  }
  return request({
    url: url,
    isFormData: true,
    method: 'post',
    data: data
  })
}

// 数据字典--错误信息
export const getDictError = (params) => {
  const data = Object.assign({}, {
    spell: '',
    number: 1000,
  }, params)
  return request({
    url: 'eis.simple?service=eamPageParamQueryService&method=getEamErrorInfoSelect',
    isFormData: true,
    method: 'post',
    data: data,
  })
}
// 数据字典--参数
export function getWorkParam(data) {
  return request({
    url: 'eis.simple?service=eamPageParamQueryService&method=queryAssetParamXmSelected',
    method: 'post',
    isFormData: true,
    data: data
  });
}
// 数据字典--物料名称
export function getMaterialName(data) {
  return request({
    // url: 'eis.simple?service=eamPageParamQueryService&method=queryMaterialsXmSelected',
    url: 'eis.simple?service=eamPageParamQueryService&method=queryMobileMaterialsXmSelected',
    method: 'post',
    isFormData: true,
    data: data
  });
}

// 选择人员
export const getPerson = (data) => {
  return request({
    url: 'eis.simple?service=pageParamQueryService&method=searchusercode',
    method: 'post',
    isFormData: true,
    data: data
  });
}

// 扫码
export const scanCodeAsset = (data) => {
  return request({
    url: 'eis.simple?service=eamOrderService&method=queryAssetByScan',
    method: 'post',
    // isFormData: true,
    data: data
  });
}
